package com.zodiac.alioth.web.controller;

import com.alibaba.fastjson.JSON;
import com.zodiac.alioth.common.annotation.AuthCheck;
import com.zodiac.alioth.common.annotation.NotLogResults;
import com.zodiac.alioth.common.util.OkHttpUtil;
import com.zodiac.alioth.dal.model.MaoDemoDO;
import com.zodiac.alioth.dal.mapper.MaoDemoDOMapper;
import com.zodiac.alioth.service.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author zodiac
 */
@Slf4j
@RestController
public class IndexController {

    @Resource(name = "maoDemoDOMapper")
    private MaoDemoDOMapper maoDemoDOMapper;

    @Resource
    private RedisService redisService;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        log.info("HeaderNames={}", JSON.toJSONString(request.getHeaderNames()));
        String contextPath = request.getContextPath();
        log.info("contextPath={}", contextPath);
        Enumeration<String> parameterNames = request.getParameterNames();
        Map<String, Object> resultMap = new HashMap<>();
        if (Objects.nonNull(parameterNames)) {
            while (parameterNames.hasMoreElements()) {
                String key = parameterNames.nextElement();
                resultMap.put(key, request.getParameter(key));
            }
        }
        if (resultMap.size() == 0) {
            resultMap.put("default", "no Data");
        }
        String result = JSON.toJSONString(resultMap);
        log.info(result);
        return result;
    }

    @RequestMapping("/mao/list")
    public String getMaoDemoList() {
        List<MaoDemoDO> maoDemoDOS = maoDemoDOMapper.selectList(0L, 100L);
        String result = JSON.toJSONString(maoDemoDOS);
        log.info(result);
        return result;
    }

    @RequestMapping("/mao/cache/get")
    public String getMaoDemoById(HttpServletRequest request, HttpServletResponse response) {
        String idStr = request.getParameter("id");
        if (StringUtils.isEmpty(idStr)) {
            return "id is null";
        }
        Long id = Long.parseLong(idStr);
        MaoDemoDO maoDemoDO = maoDemoDOMapper.selectByPrimaryKey(id);
        return JSON.toJSONString(maoDemoDO);
    }

    @AuthCheck
    @RequestMapping("/redis/add")
    public String cacheAdd(String key, String value) {
        redisService.set(key, value);
        return key + ":" + value;
    }

    @RequestMapping("/redis/get")
    @NotLogResults
    public String cacheGet(String key) {
        Object o = redisService.get(key);
        return JSON.toJSONString(o);
    }

    /**
     * 查看okhttp配置
     *
     * @param key
     * @return
     */
    @RequestMapping("/okhttp/get")
    @NotLogResults
    public String getOkhttp(String key) {
        OkHttpClient okHttpClient = OkHttpUtil.getOkHttpClient();
        int readTimeoutMillis = okHttpClient.readTimeoutMillis();
        int writeTimeoutMillis = okHttpClient.writeTimeoutMillis();
        int connectTimeoutMillis = okHttpClient.connectTimeoutMillis();
        ConnectionPool connectionPool = okHttpClient.connectionPool();
        int connectionCount = connectionPool.connectionCount();
        Map<String,Integer> map = new HashMap<>();
        map.put("readTimeoutMillis", readTimeoutMillis);
        map.put("writeTimeoutMillis", writeTimeoutMillis);
        map.put("connectTimeoutMillis", connectTimeoutMillis);
        map.put("connectionCount", connectionCount);
        return JSON.toJSONString(map);
    }

    /**
     * 修改okhttp配置
     *
     * @param key
     * @return
     */
    @RequestMapping("/update/okhttp")
    @NotLogResults
    public String updateOkhttp(String key) {
        ConnectionPool connectionPool = new ConnectionPool(200, 5, TimeUnit.SECONDS);

        OkHttpClient okHttpClient = OkHttpUtil.initOkHttpClient(20, 10, 30, connectionPool);
        return JSON.toJSONString(okHttpClient);
    }


}
